package cliente_diseno2011;

import javax.print.DocFlavor.URL;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JLabel;
import java.awt.Checkbox;
import javax.swing.JToggleButton;
import javax.swing.JCheckBox;
import javax.swing.JSlider;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.List;

import javax.swing.SpringLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import AdministradorConfiguracion.*;

public class ListaPost extends JPanel {

	private String eleccion;
	private JPanel panel;
	private int index;
	private ArrayList<Candidato> post;
	JButton btnVerMsPostulantes;
	JButton btnSiguiente;
	JButton btnAntPostulantes;
	private JLabel lblNewLabel_1;
	JRadioButton rdbtnNewRadioButton;
	JRadioButton rdbtnNewRadioButton_1;
	JRadioButton rdbtnNewRadioButton_2;
	JRadioButton rdbtnNewRadioButton_3;
	JRadioButton rdbtnNewRadioButton_4;
	JRadioButton rdbtnNewRadioButton_5;
	JRadioButton rdbtnNewRadioButton_6;
	JRadioButton rdbtnNewRadioButton_7;
	JPanel panel_1;
	ArrayList<JRadioButton> botones;
	ArrayList<ArrayList<String>> candidatos;

	ArrayList<Image> imagenes;
	ButtonGroup group;
	JButton btnAnterior;

	public ListaPost(Etapa e, boolean antEtapas, boolean sigEtapas) {

	
		panel = new JPanel();
		botones = new ArrayList<JRadioButton>();
		JLabel lblNewLabel = new JLabel(e.getNombre());
		post = e.getCandidatos();

		btnVerMsPostulantes = new JButton("Sig postulantes");
		btnVerMsPostulantes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				int j = 0;
				while (j < 8 && index <= post.size() - 1) {

					botones.get(j).setText(post.get(index).toString());

					index++;
					j++;

				}
				while (j < 8) {
					botones.get(j).setVisible(false);
					j++;
				}

				if (index + 1 < post.size()) {
					btnVerMsPostulantes.setEnabled(true);
				} else {
					btnVerMsPostulantes.setEnabled(false);
				}
				btnAntPostulantes.setEnabled(true);
				group.clearSelection();
				eleccion = null;

			}
		});

		btnAnterior = new JButton("Anterior etapa");
		btnAnterior.setEnabled(antEtapas);
		btnSiguiente = new JButton("Siguiente etapa");
		btnSiguiente.setEnabled(sigEtapas);

		btnAntPostulantes = new JButton("Ant postulantes");
		btnAntPostulantes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				btnVerMsPostulantes.setEnabled(true);

				int k = 0;
				System.out.println("el index es " + index);
				if ((index - (int) (index / 8) * 8) == 0) {
					index = index - 16;
				} else {
					index = index - (index - (int) (index / 8) * 8) - 8;
				}

		
				while (k < 8 && index <= post.size() - 1) {
				
					botones.get(k).setText(post.get(index).toString());
					
					botones.get(k).setVisible(true);
					index++;
					k++;
				}

				if (index - 8 > 0) {
					btnAntPostulantes.setEnabled(true);
				} else {
					btnAntPostulantes.setEnabled(false);
				}
				group.clearSelection();
				eleccion = null;
			}

		});

		lblNewLabel_1 = new JLabel("");

	

		imagenes = new ArrayList<Image>();
	
		panel_1 = new JPanel();
		panel_1.setLayout(new BorderLayout(0, 0));
	

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout
				.createParallelGroup(Alignment.TRAILING)
				.addGroup(
						groupLayout.createSequentialGroup().addGap(23)
								.addComponent(lblNewLabel_1)
								.addContainerGap(759, Short.MAX_VALUE))
				.addGroup(
						Alignment.LEADING,
						groupLayout
								.createSequentialGroup()
								.addGap(50)
								.addComponent(panel,
										GroupLayout.PREFERRED_SIZE, 468,
										GroupLayout.PREFERRED_SIZE)
								.addGap(80)
								.addComponent(panel_1,
										GroupLayout.PREFERRED_SIZE, 145,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(39, Short.MAX_VALUE))
				.addGroup(
						groupLayout
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(btnAnterior)
								.addPreferredGap(ComponentPlacement.RELATED,
										544, Short.MAX_VALUE)
								.addComponent(btnSiguiente).addGap(18))
				.addGroup(
						groupLayout.createSequentialGroup().addGap(337)
								.addComponent(lblNewLabel)
								.addContainerGap(390, Short.MAX_VALUE))
				.addGroup(
						groupLayout.createSequentialGroup().addGap(131)
								.addComponent(btnAntPostulantes).addGap(109)
								.addComponent(btnVerMsPostulantes)
								.addContainerGap(328, Short.MAX_VALUE)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addContainerGap(
																				81,
																				Short.MAX_VALUE)
																		.addComponent(
																				panel,
																				GroupLayout.PREFERRED_SIZE,
																				254,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								lblNewLabel_1)
																						.addComponent(
																								lblNewLabel))
																		.addGap(73)
																		.addComponent(
																				panel_1,
																				GroupLayout.PREFERRED_SIZE,
																				196,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				41,
																				Short.MAX_VALUE)))
										.addGap(23)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				btnAntPostulantes)
																		.addGap(72))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				btnVerMsPostulantes)
																		.addGap(18)
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.BASELINE)
																						.addComponent(
																								btnAnterior)
																						.addComponent(
																								btnSiguiente))
																		.addGap(31)))));
		panel.setLayout(new GridLayout(4, 4, 0, 0));

		group = new ButtonGroup();
		rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
		panel.add(rdbtnNewRadioButton_1);
		group.add(rdbtnNewRadioButton_1);
		rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_1.getText();

				System.out.println("entro a hacer la imagen ");
				Image img;
				if (rdbtnNewRadioButton_1.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();

			}
		});
		rdbtnNewRadioButton_3 = new JRadioButton("New radio button");
		rdbtnNewRadioButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_3.getText();
				Image img;
				if (rdbtnNewRadioButton_3.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();

			}
		});
		panel.add(rdbtnNewRadioButton_3);
		group.add(rdbtnNewRadioButton_3);
		rdbtnNewRadioButton_4 = new JRadioButton("New radio button");
		rdbtnNewRadioButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_4.getText();
				Image img;
				if (rdbtnNewRadioButton_4.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();

			}
		});
		panel.add(rdbtnNewRadioButton_4);
		group.add(rdbtnNewRadioButton_4);
		rdbtnNewRadioButton_7 = new JRadioButton("New radio button");
		rdbtnNewRadioButton_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_7.getText();
				Image img;
				if (rdbtnNewRadioButton_7.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();
			}
		});
		panel.add(rdbtnNewRadioButton_7);
		group.add(rdbtnNewRadioButton_7);
		rdbtnNewRadioButton_2 = new JRadioButton("New radio button");
		rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_2.getText();
				Image img;
				if (rdbtnNewRadioButton_2.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();
			}
		});
		panel.add(rdbtnNewRadioButton_2);
		group.add(rdbtnNewRadioButton_2);
		rdbtnNewRadioButton_6 = new JRadioButton("New radio button");
		rdbtnNewRadioButton_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_6.getText();
				Image img;
				if (rdbtnNewRadioButton_6.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();
			}
		});
		panel.add(rdbtnNewRadioButton_6);
		group.add(rdbtnNewRadioButton_6);
		rdbtnNewRadioButton = new JRadioButton("New radio button");
		rdbtnNewRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton.getText();
				Image img;
				if (rdbtnNewRadioButton.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();
			}
		});
		panel.add(rdbtnNewRadioButton);
		group.add(rdbtnNewRadioButton);
		rdbtnNewRadioButton_5 = new JRadioButton("New radio button");
		rdbtnNewRadioButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				eleccion = rdbtnNewRadioButton_5.getText();
				Image img;
				if (rdbtnNewRadioButton_5.getText().equals("Voto en blanco")) {
					img = new ImageIcon("./src/img/blanco.jpg").getImage();
				} else {
					img = new ImageIcon("./src/img/boleta.jpg").getImage();
				}
				BoletaPanel bol = new BoletaPanel(img);
				panel_1.add(bol);
				panel_1.validate();
			}
		});
		panel.add(rdbtnNewRadioButton_5);
		group.add(rdbtnNewRadioButton_5);

		botones.add(rdbtnNewRadioButton_1);
		botones.add(rdbtnNewRadioButton_4);
		botones.add(rdbtnNewRadioButton_2);
		botones.add(rdbtnNewRadioButton);
		botones.add(rdbtnNewRadioButton_3);

		botones.add(rdbtnNewRadioButton_7);

		botones.add(rdbtnNewRadioButton_6);

		botones.add(rdbtnNewRadioButton_5);
		setLayout(groupLayout);
		int i = 0;
		index = 0;

		while (i < 8 && index <= post.size() - 1) {

			botones.get(i).setText(post.get(index).toString());
			

			index++;
			i++;
		}
		while (i < 8) {
			botones.get(i).setVisible(false);
			i++;
		}
		btnAntPostulantes.setEnabled(false);
		if (index < post.size()) {
			btnVerMsPostulantes.setEnabled(true);
		} else {
			btnVerMsPostulantes.setEnabled(false);
		}

	}

	public void addPostSigListener(ActionListener al) {
		btnSiguiente.addActionListener(al);

	}

	public void addPostAntListener(ActionListener al) {
		btnAnterior.addActionListener(al);

	}

	public String getEleccion() {
		return eleccion;
	}
}
